package com.zz.hhbl.ui

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.zz.framework.core.ext.printJson
import com.zz.framework.core.ext.toJson
import com.zz.framework.core.http.XBaseViewModel
import com.zz.framework.core.toast.ToastUtils
import com.zz.framework.core.utils.DeviceUtils
import com.zz.framework.core.utils.EncryptUtils
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.entity.VersionEntity
import com.zz.hhbl.model.CommonHttpRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/17 22:09.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
@HiltViewModel
internal class LaunchViewModel @Inject constructor(private var repository: CommonHttpRepository) : XBaseViewModel() {
    private val _loginState = MutableLiveData<String?>("")
    val loginState: LiveData<String?> get() = _loginState
    fun login() {
        val code = DeviceUtils.getSerialCode()
        val secret = "Lhhwl@device"
        val password = EncryptUtils.encryptHmacMD5ToString(code, secret)
        quickLaunch {
            repository.login(
                hashMapOf(
                    "username" to code,
                    "password" to password
                )
            ).onSuccess {
                GlobalCache.setToken(it)
                _loginState.postValue(it)
            }.onFailure {
                it.printJson()
                ToastUtils.show(it.toJson())
            }
        }
    }

    private val _versionState = MutableLiveData<List<VersionEntity>?>(null)
    val versionState: LiveData<List<VersionEntity>?> get() = _versionState
    fun getVersion() {
        quickLaunch {
            repository.getVersion(hashMapOf()).onSuccess {
                _versionState.postValue(it?.data)
            }.onFailure {
                it.printJson()
                ToastUtils.show(it.toJson())
            }
        }
    }
}

